package com.shop.storage.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.shop.common.core.web.page.TableDataInfo;
import com.shop.storage.vo.response.Requirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.shop.common.log.annotation.Log;
import com.shop.common.log.enums.BusinessType;
import com.shop.common.security.annotation.RequiresPermissions;
import com.shop.storage.domain.WmsPurchaseRequire;
import com.shop.storage.service.IWmsPurchaseRequireService;
import com.shop.common.core.web.controller.BaseController;
import com.shop.common.core.domain.Result;
import com.shop.common.core.utils.poi.ExcelUtil;


/**
 * 采购需求Controller
 *
 * @author czb
 * @date 2024-06-26
 */
@RestController
@RequestMapping("/require")
public class WmsPurchaseRequireController extends BaseController
{
    @Autowired
    private IWmsPurchaseRequireService wmsPurchaseRequireService;

/**
 * 查询采购需求列表
 */
@RequiresPermissions("storage:require:list")
@GetMapping("/list")
    public Result list(WmsPurchaseRequire wmsPurchaseRequire)
    {
        startPage();
        List<WmsPurchaseRequire> list = wmsPurchaseRequireService.selectWmsPurchaseRequireList(wmsPurchaseRequire);
        return getDataTable(list);
    }

    /**
     * 导出采购需求列表
     */
    @RequiresPermissions("storage:require:export")
    @Log(title = "采购需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsPurchaseRequire wmsPurchaseRequire)
    {
        List<WmsPurchaseRequire> list = wmsPurchaseRequireService.selectWmsPurchaseRequireList(wmsPurchaseRequire);
        ExcelUtil<WmsPurchaseRequire> util = new ExcelUtil<WmsPurchaseRequire>(WmsPurchaseRequire.class);
        util.exportExcel(response, list, "采购需求数据");
    }

    /**
     * 获取采购需求详细信息
     */
//    @RequiresPermissions("storage:require:query")
    @GetMapping(value = "/{requireId}")
    public Result getInfo(@PathVariable("requireId") Long requireId)
    {
        return success(wmsPurchaseRequireService.selectWmsPurchaseRequireByRequireId(requireId));
    }


    @GetMapping
    public Result getInfo()
    {
        return success(wmsPurchaseRequireService.selectWmsPurchaseRequireByRequireId(1L));
    }

    /**
     * 新增采购需求
     */
    @RequiresPermissions("storage:require:add")
    @Log(title = "采购需求", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody WmsPurchaseRequire wmsPurchaseRequire)
    {
        return toAjax(wmsPurchaseRequireService.insertWmsPurchaseRequire(wmsPurchaseRequire));
    }

    /**
     * 修改采购需求
     */
    @RequiresPermissions("storage:require:edit")
    @Log(title = "采购需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody WmsPurchaseRequire wmsPurchaseRequire)
    {
        return toAjax(wmsPurchaseRequireService.updateWmsPurchaseRequire(wmsPurchaseRequire));
    }

    /**
     * 删除采购需求
     */
    @RequiresPermissions("storage:require:remove")
    @Log(title = "采购需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{requireIds}")
    public Result remove(@PathVariable List<Long> requireIds)
    {
        return toAjax(wmsPurchaseRequireService.deleteWmsPurchaseRequireByRequireIds(requireIds));
    }
    /**
     * 采购需求列表
     */

    @RequiresPermissions("storage:require:list")
    @GetMapping("/listRequire/{skuIds}/{skuName}")
    public Result<TableDataInfo> listRequire(@PathVariable("skuIds")String skuIds , @PathVariable("skuName") String skuName){
        List<Requirement> requirementList = wmsPurchaseRequireService.selectWmsPurchaseRequireLists(skuIds,skuName);
        return getDataTable(requirementList);
    }

    /**
     * 批量添加采购需求
     */
    @RequiresPermissions("storage:purchaseRequire:add")
    @PostMapping("/addRequire")
    public Result addRequire(@RequestBody Long[] wareSkuIds){
        return toAjax(wmsPurchaseRequireService.insertWmsPurchaseRequires(wareSkuIds));
    }
}
